package org.fujene.search;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import org.fujene.structure.ContentInfo;

public class SearchResultPackage implements Iterable<SearchResult>{
	private SearchPackage query;
	private ContentInfo contentInfo;
	private TreeSet<SearchResult> results;

	private SearchResultPackage(){}

	public static SearchResultPackage make(ContentInfo contentInfo, SearchPackage query){
		SearchResultPackage pkg = new SearchResultPackage();

		pkg.contentInfo = contentInfo;
		pkg.query = query;
//		pkg.results = new TreeSet<SearchResult>(new Comparator<SearchResult>(){
//			@Override
//			public int compare(SearchResult o1, SearchResult o2){
//				if(o1.score() > o2.score()) return 1;
//				else if(o1.score() < o2.score()) return -1;
//				return 0;
//			}
//		});
		pkg.results = new TreeSet<SearchResult>(new Comparator<SearchResult>(){
			@Override
			public int compare(SearchResult o1, SearchResult o2){
				if(o1.index() > o2.index()) return 1;
				else if(o1.index() < o2.index()) return -1;
				return 0;
			}
		});

		return pkg;
	}
	
	public SearchPackage query() {
		return query;
	}

	public void setResult(SearchResult result, int rank){
		result.setIndex(rank);
		this.results.add(result);
//		System.out.println("[DEBUG]Size: " + this.results.size());
	}

	public SearchResult[] getResults(){
		return results.toArray(new SearchResult[0]);
	}
	
	public int from() {
		return query.from();
	}
	
	public int total() {
		return query.length();
	}
	
	public int length() {
		return results.size();
	}
	
	public int getArraySize() {
		return (contentInfo.contentsNum() + contentInfo.appendixNum()) * results.size();
	}

	@Override
	public Iterator<SearchResult> iterator(){
		return results.iterator();
	}
}
